Skip to content

Commit

Permalink
Remove some more unused Py2 code (cythonGH-6233)
Browse files Browse the repository at this point in the history
  • Loading branch information
da-woods authored Jun 9, 2024
1 parent bcef509 commit 50fb5cf
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 53 deletions.
26 changes: 3 additions & 23 deletions Cython/Compiler/ModuleNode.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ def generate_module_preamble(self, env, options, cimported_modules, metadata, co
code.putln('#define __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT 1')
else:
code.putln('#define __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT '
'(PY_MAJOR_VERSION >= 3 && __PYX_DEFAULT_STRING_ENCODING_IS_UTF8)')
'__PYX_DEFAULT_STRING_ENCODING_IS_UTF8')
code.putln('#define __PYX_DEFAULT_STRING_ENCODING "%s"' % c_string_encoding)
if c_string_type == 'bytearray':
c_string_func_name = 'ByteArray'
Expand Down Expand Up @@ -3192,11 +3192,6 @@ def generate_module_init_func(self, imported_modules, env, code):
code.putln("stringtab_initialized = 1;")
code.put_error_if_neg(self.pos, "__Pyx_InitGlobals()") # calls any utility code

code.putln("#if PY_MAJOR_VERSION < 3 && (__PYX_DEFAULT_STRING_ENCODING_IS_ASCII || "
"__PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT)")
code.put_error_if_neg(self.pos, "__Pyx_init_sys_getdefaultencoding_params()")
code.putln("#endif")

code.putln("if (%s) {" % self.is_main_module_flag_cname())
code.put_error_if_neg(self.pos, 'PyObject_SetAttr(%s, %s, %s)' % (
env.module_cname,
Expand Down Expand Up @@ -3309,10 +3304,8 @@ def generate_module_init_func(self, imported_modules, env, code):

code.putln("#if CYTHON_PEP489_MULTI_PHASE_INIT")
code.putln("return (%s != NULL) ? 0 : -1;" % env.module_cname)
code.putln("#elif PY_MAJOR_VERSION >= 3")
code.putln("return %s;" % env.module_cname)
code.putln("#else")
code.putln("return;")
code.putln("return %s;" % env.module_cname)
code.putln("#endif")
code.putln('}')

Expand Down Expand Up @@ -3434,7 +3427,6 @@ def generate_module_import_setup(self, env, code):
if fq_module_name.endswith('.__init__'):
fq_module_name = EncodedString(fq_module_name[:-len('.__init__')])
fq_module_name_cstring = fq_module_name.as_c_string_literal()
code.putln("#if PY_MAJOR_VERSION >= 3")
code.putln("{")
code.putln("PyObject *modules = PyImport_GetModuleDict(); %s" %
code.error_goto_if_null("modules", self.pos))
Expand All @@ -3443,7 +3435,6 @@ def generate_module_import_setup(self, env, code):
fq_module_name_cstring, env.module_cname), self.pos))
code.putln("}")
code.putln("}")
code.putln("#endif")

def generate_module_cleanup_func(self, env, code):
if not Options.generate_cleanup_code:
Expand Down Expand Up @@ -3576,7 +3567,6 @@ def generate_pymoduledef_struct(self, env, code):
cleanup_func = 'NULL'

code.putln("")
code.putln("#if PY_MAJOR_VERSION >= 3")
code.putln("#if CYTHON_PEP489_MULTI_PHASE_INIT")
exec_func_cname = self.module_init_func_cname()
code.putln("static PyObject* %s(PyObject *spec, PyModuleDef *def); /*proto*/" %
Expand Down Expand Up @@ -3631,7 +3621,6 @@ def generate_pymoduledef_struct(self, env, code):
code.putln('#ifdef __cplusplus')
code.putln('} /* anonymous namespace */')
code.putln('#endif')
code.putln("#endif")

def generate_module_creation_code(self, env, code):
# Generate code to create the module object and
Expand All @@ -3647,16 +3636,7 @@ def generate_module_creation_code(self, env, code):
Naming.pymodinit_module_arg))
code.put_incref(env.module_cname, py_object_type, nanny=False)
code.putln("#else")
code.putln("#if PY_MAJOR_VERSION < 3")
code.putln(
'%s = Py_InitModule4(%s, %s, %s, 0, PYTHON_API_VERSION); Py_XINCREF(%s);' % (
env.module_cname,
env.module_name.as_c_string_literal(),
env.method_table_cname,
doc,
env.module_cname))
code.putln(code.error_goto_if_null(env.module_cname, self.pos))
code.putln("#elif CYTHON_USE_MODULE_STATE")
code.putln("#if CYTHON_USE_MODULE_STATE")
# manage_ref is False (and refnanny calls are omitted) because refnanny isn't yet initialized
module_temp = code.funcstate.allocate_temp(py_object_type, manage_ref=False)
code.putln(
Expand Down
30 changes: 0 additions & 30 deletions Cython/Utility/TypeConversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,36 +173,6 @@ static CYTHON_INLINE Py_hash_t __Pyx_PyIndex_AsHash_t(PyObject*);
#define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_DecodeUTF8(c_str, size, NULL)
#else
#define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_Decode(c_str, size, __PYX_DEFAULT_STRING_ENCODING, NULL)

// __PYX_DEFAULT_STRING_ENCODING is either a user provided string constant
// or we need to look it up here
#if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT
#include <string.h>

static char* __PYX_DEFAULT_STRING_ENCODING;

static int __Pyx_init_sys_getdefaultencoding_params(void) {
PyObject* sys;
PyObject* default_encoding = NULL;
char* default_encoding_c;

sys = PyImport_ImportModule("sys");
if (!sys) goto bad;
default_encoding = PyObject_CallMethod(sys, "getdefaultencoding", NULL);
Py_DECREF(sys);
if (!default_encoding) goto bad;
default_encoding_c = PyBytes_AsString(default_encoding);
if (!default_encoding_c) goto bad;
__PYX_DEFAULT_STRING_ENCODING = (char*) malloc(strlen(default_encoding_c) + 1);
if (!__PYX_DEFAULT_STRING_ENCODING) goto bad;
strcpy(__PYX_DEFAULT_STRING_ENCODING, default_encoding_c);
Py_DECREF(default_encoding);
return 0;
bad:
Py_XDECREF(default_encoding);
return -1;
}
#endif
#endif

/////////////// TypeConversions ///////////////
Expand Down

0 comments on commit 50fb5cf

Please sign in to comment.