Permalink
Browse files

Added distutils hook

  • Loading branch information...
mitsuhiko committed Mar 4, 2012
1 parent 3ded2c3 commit f9df51a3b8dfd9d133dd660a989f8c2769c79e84
View
5 README
@@ -0,0 +1,5 @@
+Unicode Literals PEP.
+
+This PEP is now on the central index:
+
+http://www.python.org/dev/peps/pep-0414/
@@ -0,0 +1,2 @@
+build
+dist
@@ -0,0 +1,7 @@
+from distutils.core import setup
+from unicode_unprefix.installhook import build_py_strip_unicode
+
+setup(
+ py_modules=['unicodetest'],
+ cmdclass={'build_py': build_py_strip_unicode}
+)
@@ -0,0 +1,14 @@
+print(u'Hello World!')
+
+for char in u'Hello World!':
+ print char
+
+x = {
+ '0': ur'quiet',
+ '1': uR'louder but raw',
+ '2': Ur'the other way round',
+ 'x': u'Aha',
+ 'y': u'Awesome',
+ 'z': U'I AM SHOUTING',
+ 'w': UR'SO MAD'
+}
No changes.
@@ -0,0 +1,45 @@
+from distutils.command.build_py import build_py
+from . import tokenize
+
+
+def strip_unicode_from_iterable(iterable):
+ for token in iterable:
+ if token[0] == tokenize.STRING:
+ yield tokenize.TokenInfo(tokenize.STRING, token[1].lstrip('uU'),
+ *token[2:])
+ else:
+ yield token
+
+
+class build_py_strip_unicode(build_py):
+
+ def run(self):
+ self.updated_files = []
+
+ if self.py_modules:
+ self.build_modules()
+
+ if self.py_modules:
+ self.build_modules()
+ if self.packages:
+ self.build_packages()
+ self.build_package_data()
+
+ for filename in self.updated_files:
+ self.strip_unicode_in_file(filename)
+
+ self.byte_compile(self.get_outputs(include_bytecode=0))
+
+ def strip_unicode_in_file(self, filename):
+ with open(filename, 'rb') as f:
+ tokens = tokenize.tokenize(f.readline)
+ tokens = list(strip_unicode_from_iterable(tokens))
+ with open(filename, 'wb') as f:
+ f.write(tokenize.untokenize(tokens))
+
+ def build_module(self, module, module_file, package):
+ res = build_py.build_module(self, module, module_file, package)
+ if res[1]:
+ # file was copied
+ self.updated_files.append(res[0])
+ return res
Oops, something went wrong.

0 comments on commit f9df51a

Please sign in to comment.