/*###########################################################################
#
# Copyright (c) 2003 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
############################################################################*/
#include "Python.h"
#include "structmember.h"
#define TYPE(O) ((PyTypeObject*)(O))
#define OBJECT(O) ((PyObject*)(O))
#define CLASSIC(O) ((PyClassObject*)(O))
#ifndef PyVarObject_HEAD_INIT
#define PyVarObject_HEAD_INIT(a, b) PyObject_HEAD_INIT(a) b,
#endif
#ifndef Py_TYPE
#define Py_TYPE(o) ((o)->ob_type)
#endif
#if PY_MAJOR_VERSION >= 3
#define PY3K
#endif
static PyObject *str__dict__, *str__implemented__, *strextends;
static PyObject *BuiltinImplementationSpecifications, *str__provides__;
static PyObject *str__class__, *str__providedBy__;
static PyObject *empty, *fallback, *str_implied, *str_cls, *str_implements;
static PyObject *str__conform__, *str_call_conform, *adapter_hooks;
static PyObject *str_uncached_lookup, *str_uncached_lookupAll;
static PyObject *str_uncached_subscriptions;
static PyObject *str_registry, *strro, *str_generation, *strchanged;
static PyTypeObject *Implements;
static int imported_declarations = 0;
static int
import_declarations(void)
{
PyObject *declarations, *i;
declarations = PyImport_ImportModule("zope.interface.declarations");
if (declarations == NULL)
return -1;
BuiltinImplementationSpecifications = PyObject_GetAttrString(
declarations, "BuiltinImplementationSpecifications");
if (BuiltinImplementationSpecifications == NULL)
return -1;
empty = PyObject_GetAttrString(declarations, "_empty");
if (empty == NULL)
return -1;
fallback = PyObject_GetAttrString(declarations, "implementedByFallback");
if (fallback == NULL)
return -1;
i = PyObject_GetAttrString(declarations, "Implements");
if (i == NULL)
return -1;
if (! PyType_Check(i))
{
PyErr_SetString(PyExc_TypeError,
"zope.interface.declarations.Implements is not a type");
return -1;
}
Implements = (PyTypeObject *)i;
Py_DECREF(declarations);
imported_declarations = 1;
return 0;
}
static PyTypeObject SpecType; /* Forward */
static PyObject *
implementedByFallback(PyObject *cls)
{
if (imported_declarations == 0 && import_declarations() < 0)
return NULL;
return PyObject_CallFunctionObjArgs(fallback, cls, NULL);
}
static PyObject *
implementedBy(PyObject *ignored, PyObject *cls)
{
/* Fast retrieval of implements spec, if possible, to optimize
common case. Use fallback code if we get stuck.
*/
PyObject *dict = NULL, *spec;
if (PyType_Check(cls))
{
dict = TYPE(cls)->tp_dict;
Py_XINCREF(dict);
}
if (dict == NULL)
dict = PyObject_GetAttr(cls, str__dict__);
if (dict == NULL)
{
/* Probably a security proxied class, use more expensive fallback code */
PyErr_Clear();
return implementedByFallback(cls);
}
spec = PyObject_GetItem(dict, str__implemented__);
Py_DECREF(dict);
if (spec)
{
if (imported_declarations == 0 && import_declarations() < 0)
return NULL;
if (PyObject_TypeCheck(spec, Implements))
return spec;
/* Old-style declaration, use more expensive fallback code */
Py_DECREF(spec);
return implementedByFallback(cls);
}
PyErr_Clear();
/* Maybe we have a builtin */
if (imported_declarations == 0 && import_declarations() < 0)
return NULL;
spec = PyDict_GetItem(BuiltinImplementationSpecifications, cls);
if (spec != NULL)
{
Py_INCREF(spec);
return spec;
}
/* We're stuck, use fallback */
return implementedByFallback(cls);
}
static PyObject *
getObjectSpecification(PyObject *ignored, PyObject *ob)
{
PyObject *cls, *result;
result = PyObject_GetAttr(ob, str__provides__);
if (result != NULL && PyObject_TypeCheck(result, &SpecType))
return result;
PyErr_Clear();
/* We do a getattr here so as not to be defeated by proxies */
cls = PyObject_GetAttr(ob, str__class__);
if (cls == NULL)
{
PyErr_Clear();
if (imported_declarations == 0 && import_declarations() < 0)
return NULL;
Py_INCREF(empty);
return empty;
}
result = implementedBy(NULL, cls);
Py_DECREF(cls);
return result;
}
static PyObject *
providedBy(PyObject *ignored, PyObject *ob)
{
PyObject *result, *cls, *cp;
result = PyObject_GetAttr(ob, str__providedBy__);
if (result == NULL)
{
PyErr_Clear();
return getObjectSpecification(NULL, ob);
}
/* We want to make sure we have a spec. We can't do a type check
because we may have a proxy, so we'll just try to get the
only attribute.
*/
if (PyObject_TypeCheck(result, &SpecType)
||
PyObject_HasAttr(result, strextends)
)
return result;
/*
The object's class doesn't understand descriptors.
Sigh. We need to get an object descriptor, but we have to be
careful. We want to use the instance's __provides__,l if
there is one, but only if it didn't come from the class.
*/
Py_DECREF(result);
cls = PyObject_GetAttr(ob, str__class__);
if (cls == NULL)
return NULL;
result = PyObject_GetAttr(ob, str__provides__);
if (result == NULL)
{
/* No __provides__, so just fall back to implementedBy */
PyErr_Clear();
result = implementedBy(NULL, cls);
Py_DECREF(cls);
return result;
}
cp = PyObject_GetAttr(cls, str__provides__);
if (cp == NULL)
{
/* The the class has no provides, assume we're done: */
PyErr_Clear();
Py_DECREF(cls);
return result;
}
if (cp == result)
{
/*
Oops, we got the provides from the class. This means
the object doesn't have it's own. We should use implementedBy
*/
Py_DECREF(result);
result = implementedBy(NULL, cls);
}
Py_DECREF(cls);
Py_DECREF(cp);
return result;
}
/*
Get an attribute from an inst dict. Return a borrowed reference.
This has a number of advantages:
- It avoids layers of Python api
- It doesn't waste time looking for descriptors
- It fails wo raising an exception, although that shouldn't really
matter.
*/
static PyObject *
inst_attr(PyObject *self, PyObject *name)
{
PyObject **dictp, *v;
dictp = _PyObject_GetDictPtr(self);
if (dictp && *dictp && (v = PyDict_GetItem(*dictp, name)))
return v;
PyErr_SetObject(PyExc_AttributeError, name);
return NULL;
}
static PyObject *
Spec_extends(PyObject *self, PyObject *other)
{
PyObject *implied;
implied = inst_attr(self, str_implied);
if (implied == NULL)
return NULL;
#ifdef Py_True
if (PyDict_GetItem(implied, other) != NULL)
{
Py_INCREF(Py_True);
return Py_True;
}
Py_INCREF(Py_False);
return Py_False;
#else
return PyInt_FromLong(PyDict_GetItem(implied, other) != NULL);
#endif
}
static char Spec_extends__doc__[] =
"Test whether a specification is or extends another"
;
static char Spec_providedBy__doc__[] =
"Test whether an interface is implemented by the specification"
;
static PyObject *
Spec_call(PyObject *self, PyObject *args, PyObject *kw)
{
PyObject *spec;
if (! PyArg_ParseTuple(args, "O", &spec))
return NULL;
return Spec_extends(self, spec);
}
static PyObject *
Spec_providedBy(PyObject *self, PyObject *ob)
{
PyObject *decl, *item;
decl = providedBy(NULL, ob);
if (decl == NULL)
return NULL;
if (PyObject_TypeCheck(decl, &SpecType))
item = Spec_extends(decl, self);
else
/*
python爬虫教程学习python爬虫,包括浏览器抓包,手机APP抓包,
需积分: 0 131 浏览量
更新于2023-12-28
收藏 25.94MB ZIP 举报
解锁网络数据的宝藏:Python爬虫工具与教程集合
一、探索网络信息的无限宝藏
在互联网的海洋中,蕴藏着海量的有价值信息。如何合法、高效地获取这些信息?Python爬虫工具与教程为您揭开这一神秘面纱。通过这些资源,您可以轻松地爬取网站信息,提取所需内容,为各种应用场景提供强大的数据支持。
二、资源亮点
工具齐全:提供一系列功能强大的Python爬虫工具,满足您不同场景下的需求。
教程详尽:配套的Python爬虫教程,从基础到进阶,让您逐步掌握爬虫的核心技术。
合法合规:严格遵守法律法规和网站使用协议,确保采集行为合法,尊重网站权益。
实战项目:结合实际案例,让您在实践中掌握Python爬虫的运用,真正做到学以致用。
三、适用人群
无论您是数据分析师、网络开发者还是对Python爬虫感兴趣的爱好者,这些资源都将为您的学习和实践提供有力的支持。
四、使用建议
按需选择工具与教程:根据实际需求选择合适的工具和教程,确保学习与实践的有效性。
遵守法律法规与协议:在使用这些资源进行爬取活动时,务必遵守相关法律法规和网站的使用协议。
持续学习与更新:随着网络技术的不断进步,Python爬虫技术也在不断发展。建议您持续关注相关动态,提升自己的技能水平。
五、安全与责任
尊重网站权益:避免对目标网站的正常运行造成干扰或损害,合理使用资源。
隐私保护:在采集数据时,严格遵守隐私保护法规,不泄露或滥用用户个人信息。
风险防范:了解并应对潜在的网络威胁,采取相应措施降低风险。
感谢您选择我们的Python爬虫工具与教程集合!让我们一起挖掘网络信息的宝藏,为您的工作和研究注入新的活力!请务必遵守法律法规和网站使用协议,共同维护网络数据的合法采集与利用。

chinacha_
- 粉丝: 2272
最新资源
- 多媒体技术在高职计算机教学中的问题及其对策探讨.docx
- 新技术领域-区块链数字资产支付.docx
- 单片机电子闹钟设计方案.doc
- 计算机操作系统.ppt
- 全国计算机三级《数据库技术》模拟试题.doc
- 基于翻转课堂的计算机应用基础教学改革浅析.docx
- 情境探究教学建构深度学习的实践探索.docx
- 单片机的家用加湿器控制装置研究与设计开发.doc
- 人工智能翻译应用前景分析.docx
- 万能铣床电气及PLC控制系统设计.doc
- 基于单片机的数字温度计方案设计书(附代码及仿真).doc
- 面向监控应用的嵌入式网络技术研究.doc
- 财务软件方案.docx
- 《软件无线电数字调制解调技术研究》开题报告和任务书.doc
- 综合布线类项目施工图解.doc
- WEB方式的无线仓储管理解决实施方案.doc