From 1477189b9ae420a0a0c46af600d2f54d98336d77 Mon Sep 17 00:00:00 2001 From: ndparker Date: Sun, 16 Nov 2014 15:33:53 +0100 Subject: [PATCH] add pypy3 support --- _setup/include/cext.h | 56 +++++++++++++++++++++++++++++++++++----- _setup/py2/dev/apidoc.py | 4 +-- _setup/py2/setup.py | 16 ++++++------ _setup/py2/term/_term.py | 9 +++---- _setup/py3/setup.py | 26 +++++++++++-------- _setup/py3/term/_term.py | 14 +++++----- 6 files changed, 83 insertions(+), 42 deletions(-) diff --git a/_setup/include/cext.h b/_setup/include/cext.h index 47b6f5b..78fa7f5 100644 --- a/_setup/include/cext.h +++ b/_setup/include/cext.h @@ -62,6 +62,18 @@ #define EXT3 +#ifndef Py_TPFLAGS_HAVE_CLASS +#define Py_TPFLAGS_HAVE_CLASS (0) +#endif + +#ifndef Py_TPFLAGS_HAVE_WEAKREFS +#define Py_TPFLAGS_HAVE_WEAKREFS (0) +#endif + +#ifndef Py_TPFLAGS_HAVE_ITER +#define Py_TPFLAGS_HAVE_ITER (0) +#endif + #ifndef PyMODINIT_FUNC #define EXT_INIT_FUNC PyObject *CONCATENATE(PyInit_, EXT_MODULE)(void) #else @@ -85,10 +97,17 @@ static struct PyModuleDef EXT_DEFINE_VAR = { \ #define EXT_INIT_ERROR(module) do {Py_XDECREF(module); return NULL;} while(0) #define EXT_INIT_RETURN(module) return module +#define EXT_DOC_UNICODE(m) + #else /* end py3k */ #define EXT2 +#ifndef PyVarObject_HEAD_INIT + #define PyVarObject_HEAD_INIT(type, size) \ + PyObject_HEAD_INIT(type) size, +#endif + #ifndef PyMODINIT_FUNC #define EXT_INIT_FUNC void CONCATENATE(init, EXT_MODULE)(void) #else @@ -117,6 +136,25 @@ static struct EXT_DEFINE__STRUCT EXT_DEFINE_VAR = { \ #define EXT_INIT_ERROR(module) return #define EXT_INIT_RETURN(module) return +#define EXT_DOC_UNICODE(m) do { \ + PyObject *doc__, *uni__; \ + int res__; \ + \ + if ((doc__ = PyObject_GetAttrString(m, "__doc__"))) { \ + uni__ = PyUnicode_FromEncodedObject(doc__, "utf-8", "strict"); \ + Py_DECREF(doc__); \ + if (!uni__) \ + EXT_INIT_ERROR(m); \ + res__ = PyObject_SetAttrString(m, "__doc__", uni__); \ + Py_DECREF(uni__); \ + if (res__ == -1) \ + EXT_INIT_ERROR(m); \ + } \ + else if (!(PyErr_Occurred() \ + && PyErr_ExceptionMatches(PyExc_AttributeError))) \ + EXT_INIT_ERROR(m); \ +} while(0) + #endif /* end py2K */ #define EXT_INIT_TYPE(module, type) do { \ @@ -232,13 +270,17 @@ typedef int Py_ssize_t; #define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC) #endif -#define DEFINE_GENERIC_DEALLOC(prefix) \ -static void prefix##_dealloc(void *self) \ -{ \ - if (PyType_IS_GC(((PyObject *)self)->ob_type)) \ - PyObject_GC_UnTrack(self); \ - (void)prefix##_clear(self); \ - ((PyObject *)self)->ob_type->tp_free((PyObject *)self); \ +#ifndef Py_TYPE +#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) +#endif + +#define DEFINE_GENERIC_DEALLOC(prefix) \ +static void prefix##_dealloc(void *self) \ +{ \ + if (PyType_IS_GC(Py_TYPE(self))) \ + PyObject_GC_UnTrack(self); \ + (void)prefix##_clear(self); \ + (Py_TYPE(self))->tp_free((PyObject *)self); \ } #endif /* SETUP_CEXT_H */ diff --git a/_setup/py2/dev/apidoc.py b/_setup/py2/dev/apidoc.py index 56904de..63a4669 100644 --- a/_setup/py2/dev/apidoc.py +++ b/_setup/py2/dev/apidoc.py @@ -41,7 +41,7 @@ def _cleanup_epydoc(target): """ search = _re.compile(r']+width="100%%"').search for filename in _shell.files(target, '*.html'): - fp = open(filename, 'r', encoding='latin-1') + fp = open(filename, 'r') try: html = fp.read() finally: @@ -53,7 +53,7 @@ def _cleanup_epydoc(target): if end >= 0: end += len('') + 1 html = html[:start] + html[end:] - fp = open(filename, 'w', encoding='latin-1') + fp = open(filename, 'w') try: fp.write(html) finally: diff --git a/_setup/py2/setup.py b/_setup/py2/setup.py index 934d806..b2fcde8 100644 --- a/_setup/py2/setup.py +++ b/_setup/py2/setup.py @@ -87,7 +87,7 @@ def find_description(docs): summary = None filename = docs.get('meta.summary', 'SUMMARY').strip() if filename and _os.path.isfile(filename): - fp = open(filename, encoding='utf-8') + fp = open(filename) try: try: summary = fp.read().strip().splitlines()[0].rstrip() @@ -99,7 +99,7 @@ def find_description(docs): description = None filename = docs.get('meta.description', 'DESCRIPTION').strip() if filename and _os.path.isfile(filename): - fp = open(filename, encoding='utf-8') + fp = open(filename) try: description = fp.read().rstrip() finally: @@ -125,7 +125,7 @@ def find_classifiers(docs): """ filename = docs.get('meta.classifiers', 'CLASSIFIERS').strip() if filename and _os.path.isfile(filename): - fp = open(filename, encoding='utf-8') + fp = open(filename) try: content = fp.read() finally: @@ -144,7 +144,7 @@ def find_provides(docs): """ filename = docs.get('meta.provides', 'PROVIDES').strip() if filename and _os.path.isfile(filename): - fp = open(filename, encoding='utf-8') + fp = open(filename) try: content = fp.read() finally: @@ -163,7 +163,7 @@ def find_license(docs): """ filename = docs.get('meta.license', 'LICENSE').strip() if filename and _os.path.isfile(filename): - fp = open(filename, encoding='utf-8') + fp = open(filename) try: return fp.read().rstrip() finally: @@ -338,7 +338,7 @@ def run(config=('package.cfg',), ext=None, script_args=None, manifest_only=0): ext = [] cfg = _util.SafeConfigParser() - cfg.read(config, encoding='utf-8') + cfg.read(config) pkg = dict(cfg.items('package')) python_min = pkg.get('python.min') or None python_max = pkg.get('python.max') or None @@ -368,13 +368,13 @@ def run(config=('package.cfg',), ext=None, script_args=None, manifest_only=0): keywords = keywords.split() revision = pkg.get('version.revision', '').strip() if revision: - revision = "-r%s" % (revision,) + revision = int(revision) kwargs = { 'name': pkg['name'], 'version': "%s%s" % ( pkg['version.number'], - ["", "-dev%s" % (revision,)][_util.humanbool( + ["", ".dev%d" % (revision,)][_util.humanbool( 'version.dev', pkg.get('version.dev', 'false') )], ), diff --git a/_setup/py2/term/_term.py b/_setup/py2/term/_term.py index 72b727c..ad3ca95 100644 --- a/_setup/py2/term/_term.py +++ b/_setup/py2/term/_term.py @@ -1,6 +1,6 @@ # -*- coding: ascii -*- # -# Copyright 2007, 2008, 2009, 2010, 2011 +# Copyright 2007 - 2015 # Andr\xe9 Malo or his licensors, as applicable # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -54,8 +54,7 @@ class _INFO(dict): """ Make color control string """ seq = _curses.tigetstr('setaf') if seq is not None: - # XXX may fail - need better logic - seq = seq.replace("%p1", "") % color + seq = _curses.tparm(seq, color) return seq self['NORMAL'] = _curses.tigetstr('sgr0') @@ -71,7 +70,7 @@ class _INFO(dict): def __getitem__(self, key): """ Deliver always """ - dict.get(self, key) or "" + return dict.get(self, key) or "" def terminfo(): @@ -111,5 +110,3 @@ def announce(fmt, **kwargs): write(fmt, **kwargs) _sys.stdout.write("\n") _sys.stdout.flush() - - diff --git a/_setup/py3/setup.py b/_setup/py3/setup.py index a759e16..8a30540 100644 --- a/_setup/py3/setup.py +++ b/_setup/py3/setup.py @@ -250,14 +250,14 @@ def make_manifest(manifest, config, docs, kwargs): kwargs['packages'] = list(kwargs.get('packages') or ()) + [ '_setup', '_setup.py2', '_setup.py3', ] + list(manifest.get('packages.extra', '').split() or ()) - _core._setup_stop_after = "commandline" + _core._setup_stop_after = "commandline" # noqa pylint: disable = protected-access try: dist = _core.setup(**kwargs) finally: - _core._setup_stop_after = None + _core._setup_stop_after = None # pylint: disable = protected-access result = ['MANIFEST', 'PKG-INFO', 'setup.py'] + list(config) - # TODO: work with default values: + # xx: work with default values? for key in ('classifiers', 'description', 'summary', 'provides', 'license'): filename = docs.get('meta.' + key, '').strip() @@ -266,7 +266,7 @@ def make_manifest(manifest, config, docs, kwargs): cmd = dist.get_command_obj("build_py") cmd.ensure_finalized() - #from pprint import pprint; pprint(("build_py", cmd.get_source_files())) + # from pprint import pprint; pprint(("build_py", cmd.get_source_files())) for item in cmd.get_source_files(): result.append(_posixpath.sep.join( _os.path.normpath(item).split(_os.path.sep) @@ -274,7 +274,7 @@ def make_manifest(manifest, config, docs, kwargs): cmd = dist.get_command_obj("build_ext") cmd.ensure_finalized() - #from pprint import pprint; pprint(("build_ext", cmd.get_source_files())) + # from pprint import pprint; pprint(("build_ext", cmd.get_source_files())) for item in cmd.get_source_files(): result.append(_posixpath.sep.join( _os.path.normpath(item).split(_os.path.sep) @@ -288,7 +288,7 @@ def make_manifest(manifest, config, docs, kwargs): cmd = dist.get_command_obj("build_clib") cmd.ensure_finalized() if cmd.libraries: - #import pprint; pprint.pprint(("build_clib", cmd.get_source_files())) + # import pprint; pprint.pprint(("build_clib", cmd.get_source_files())) for item in cmd.get_source_files(): result.append(_posixpath.sep.join( _os.path.normpath(item).split(_os.path.sep) @@ -301,7 +301,7 @@ def make_manifest(manifest, config, docs, kwargs): cmd = dist.get_command_obj("build_scripts") cmd.ensure_finalized() - #import pprint; pprint.pprint(("build_scripts", cmd.get_source_files())) + # import pprint; pprint.pprint(("build_scripts", cmd.get_source_files())) if cmd.get_source_files(): for item in cmd.get_source_files(): result.append(_posixpath.sep.join( @@ -310,7 +310,7 @@ def make_manifest(manifest, config, docs, kwargs): cmd = dist.get_command_obj("install_data") cmd.ensure_finalized() - #from pprint import pprint; pprint(("install_data", cmd.get_inputs())) + # from pprint import pprint; pprint(("install_data", cmd.get_inputs())) try: strings = str except NameError: @@ -335,11 +335,15 @@ def make_manifest(manifest, config, docs, kwargs): def run(config=('package.cfg',), ext=None, script_args=None, manifest_only=0): """ Main runner """ + # pylint: disable = too-many-locals if ext is None: ext = [] cfg = _util.SafeConfigParser() - cfg.read(config, encoding='utf-8') + if (3, 0, 0) <= _sys.version_info < (3, 2, 0): + cfg.read(config) + else: + cfg.read(config, encoding='utf-8') pkg = dict(cfg.items('package')) python_min = pkg.get('python.min') or None python_max = pkg.get('python.max') or None @@ -369,13 +373,13 @@ def run(config=('package.cfg',), ext=None, script_args=None, manifest_only=0): keywords = keywords.split() revision = pkg.get('version.revision', '').strip() if revision: - revision = "-r%s" % (revision,) + revision = int(revision) kwargs = { 'name': pkg['name'], 'version': "%s%s" % ( pkg['version.number'], - ["", "-dev%s" % (revision,)][_util.humanbool( + ["", ".dev%d" % (revision,)][_util.humanbool( 'version.dev', pkg.get('version.dev', 'false') )], ), diff --git a/_setup/py3/term/_term.py b/_setup/py3/term/_term.py index 4d70d1e..03f5acb 100644 --- a/_setup/py3/term/_term.py +++ b/_setup/py3/term/_term.py @@ -1,6 +1,6 @@ # -*- coding: ascii -*- # -# Copyright 2007, 2008, 2009, 2010, 2011 +# Copyright 2007 - 2015 # Andr\xe9 Malo or his licensors, as applicable # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -53,16 +53,16 @@ class _INFO(dict): try: _curses.tigetstr('sgr0') except TypeError: # pypy3 + # pylint: disable = invalid-name bc = lambda val: val.encode('ascii') else: - bc = lambda val: val + bc = lambda val: val # pylint: disable = invalid-name def make_color(color): """ Make color control string """ - seq = _curses.tigetstr(bc('setaf')).decode('ascii') + seq = _curses.tigetstr(bc('setaf')) if seq is not None: - # XXX may fail - need better logic - seq = seq.replace("%p1", "") % color + seq = _curses.tparm(seq, color).decode('ascii') return seq self['NORMAL'] = _curses.tigetstr(bc('sgr0')).decode('ascii') @@ -79,7 +79,7 @@ class _INFO(dict): def __getitem__(self, key): """ Deliver always """ - dict.get(self, key) or "" + return dict.get(self, key) or "" def terminfo(): @@ -119,5 +119,3 @@ def announce(fmt, **kwargs): write(fmt, **kwargs) _sys.stdout.write("\n") _sys.stdout.flush() - -