Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6cb2b92

Browse files
committed
convert ast versioning to mercurial
1 parent 540fcd7 commit 6cb2b92

6 files changed

Lines changed: 31 additions & 26 deletions

File tree

Doc/library/ast.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ Node classes
9696
Abstract Grammar
9797
----------------
9898

99-
The module defines a string constant ``__version__`` which is the decimal
100-
Subversion revision number of the file shown below.
99+
The module defines a string constant ``__version__`` which is the Mercurial
100+
revision of the file shown below.
101101

102102
The abstract grammar is currently defined as follows:
103103

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ What's New in Python 3.3 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- _ast.__version__ is now a Mercurial integer and hex revision.
14+
1315
- Issue #9856: Change object.__format__ with a non-empty format string
1416
to be a DeprecationWarning. In 3.2 it was a PendingDeprecationWarning.
1517
In 3.4 it will be a TypeError.

Parser/Python.asdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- ASDL's four builtin types are identifier, int, string, object
22

3-
module Python version "$Revision$"
3+
module Python
44
{
55
mod = Module(stmt* body)
66
| Interactive(stmt* body)

Parser/asdl.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,28 +114,20 @@ def error(self, tok):
114114
raise ASDLSyntaxError(tok.lineno, tok)
115115

116116
def p_module_0(self, info):
117-
" module ::= Id Id version { } "
118-
module, name, version, _0, _1 = info
117+
" module ::= Id Id { } "
118+
module, name, _0, _1 = info
119119
if module.value != "module":
120120
raise ASDLSyntaxError(module.lineno,
121121
msg="expected 'module', found %s" % module)
122-
return Module(name, None, version)
122+
return Module(name, None)
123123

124124
def p_module(self, info):
125-
" module ::= Id Id version { definitions } "
126-
module, name, version, _0, definitions, _1 = info
125+
" module ::= Id Id { definitions } "
126+
module, name, _0, definitions, _1 = info
127127
if module.value != "module":
128128
raise ASDLSyntaxError(module.lineno,
129129
msg="expected 'module', found %s" % module)
130-
return Module(name, definitions, version)
131-
132-
def p_version(self, info):
133-
"version ::= Id String"
134-
version, V = info
135-
if version.value != "version":
136-
raise ASDLSyntaxError(version.lineno,
137-
msg="expected 'version', found %" % version)
138-
return V
130+
return Module(name, definitions)
139131

140132
def p_definition_0(self, definition):
141133
" definitions ::= definition "
@@ -246,10 +238,9 @@ class AST(object):
246238
pass # a marker class
247239

248240
class Module(AST):
249-
def __init__(self, name, dfns, version):
241+
def __init__(self, name, dfns):
250242
self.name = name
251243
self.dfns = dfns
252-
self.version = version
253244
self.types = {} # maps type name to value (from dfns)
254245
for type in dfns:
255246
self.types[type.name.value] = type.value

Parser/asdl_c.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# handle fields that have a type but no name
66

77
import os, sys
8+
import subprocess
89

910
import asdl
1011

@@ -882,9 +883,6 @@ def visitConstructor(self, cons, name, simple):
882883
self.emit("if (!%s_singleton) return 0;" % cons.name, 1)
883884

884885

885-
def parse_version(mod):
886-
return mod.version.value[12:-3]
887-
888886
class ASTModuleVisitor(PickleVisitor):
889887

890888
def visitModule(self, mod):
@@ -904,7 +902,7 @@ def visitModule(self, mod):
904902
self.emit("return NULL;", 2)
905903
# Value of version: "$Revision$"
906904
self.emit('if (PyModule_AddStringConstant(m, "__version__", "%s") < 0)'
907-
% parse_version(mod), 1)
905+
% (mod.version,), 1)
908906
self.emit("return NULL;", 2)
909907
for dfn in mod.dfns:
910908
self.visit(dfn)
@@ -1137,6 +1135,18 @@ def visit(self, object):
11371135
11381136
"""
11391137

1138+
1139+
def get_file_revision(f):
1140+
"""Fish out the last change to a file in hg."""
1141+
args = ["hg", "log", "--template", "{rev}:{node|short}", "--limit", "1"]
1142+
p = subprocess.Popen(args, stdout=subprocess.PIPE)
1143+
out = p.communicate()[0]
1144+
if p.returncode:
1145+
print >> sys.stderr, "error return code from hg"
1146+
sys.exit(1)
1147+
return out
1148+
1149+
11401150
def main(srcfile):
11411151
argv0 = sys.argv[0]
11421152
components = argv0.split(os.sep)
@@ -1145,6 +1155,7 @@ def main(srcfile):
11451155
mod = asdl.parse(srcfile)
11461156
if not asdl.check(mod):
11471157
sys.exit(1)
1158+
mod.version = get_file_revision(srcfile)
11481159
if INC_DIR:
11491160
p = "%s/%s-ast.h" % (INC_DIR, mod.name)
11501161
f = open(p, "w")
@@ -1164,7 +1175,7 @@ def main(srcfile):
11641175
p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c")
11651176
f = open(p, "w")
11661177
f.write(auto_gen_msg)
1167-
f.write(c_file_msg % parse_version(mod))
1178+
f.write(c_file_msg % (mod.version,))
11681179
f.write('#include "Python.h"\n')
11691180
f.write('#include "%s-ast.h"\n' % mod.name)
11701181
f.write('\n')

Python/Python-ast.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
/*
5-
__version__ 82163.
5+
__version__ 68409:c017695acf19.
66
77
This module must be committed separately after each AST grammar change;
88
The __version__ number is set to the revision number of the commit
@@ -6739,7 +6739,8 @@ PyInit__ast(void)
67396739
NULL;
67406740
if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
67416741
return NULL;
6742-
if (PyModule_AddStringConstant(m, "__version__", "82163") < 0)
6742+
if (PyModule_AddStringConstant(m, "__version__", "68409:c017695acf19")
6743+
< 0)
67436744
return NULL;
67446745
if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return
67456746
NULL;

0 commit comments

Comments
 (0)